package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsUserSearchRecordsBiz;
import com.project.entity.CmsUserSearchRecords;
import com.project.mapper.CmsUserSearchRecordsMapper;
import com.project.pojo.model.GetUserSearchContentsModel;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 用户搜索记录业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/11/20 1:42
 * @Version: 1.0
 */
@Service
public class CmsUserSearchRecordsBizImpl extends ServiceImpl<CmsUserSearchRecordsMapper, CmsUserSearchRecords> implements CmsUserSearchRecordsBiz {

    /**
     * 分页获取用户搜索记录
     * @param getUserSearchContentsModel
     * @param userId
     * @return
     */
    public List<String> getUserSearchContents(GetUserSearchContentsModel getUserSearchContentsModel, Long userId){
        Page<CmsUserSearchRecords> page = new Page<>(getUserSearchContentsModel.getPageNo(), getUserSearchContentsModel.getPageSize());

        QueryWrapper<CmsUserSearchRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("base_user_id", userId);
        queryWrapper.select("DISTINCT search_content");
        IPage<CmsUserSearchRecords> iPage = this.baseMapper.selectPage(page, queryWrapper);
        List<String> list = new ArrayList<>();
        if (null != iPage && null != iPage.getRecords() && iPage.getRecords().size() > 0){
            for (CmsUserSearchRecords records : iPage.getRecords()){
                list.add(records.getSearchContent());
            }
        }
        return list;
    }

    /**
     * 添加搜索记录
     * @param searchContent
     * @param userId
     * @return
     */
    public int addRearchRecords(String searchContent, Long userId){
        CmsUserSearchRecords userSearchRecords = new CmsUserSearchRecords();
        userSearchRecords.setBaseUserId(userId);
        userSearchRecords.setSearchContent(searchContent);
        userSearchRecords.setCreateTime(new Date());
        return this.baseMapper.insert(userSearchRecords);
    }

}
